suppressMessages(library(Seurat))
suppressMessages(library(ggplot2))
suppressMessages(library(scater))
srt <- readRDS("/mnt/nmorais-nfs/marta/pB_joana/pC_data/srt-mono-macs-young-without-residents-bec.rds")
srt
## An object of class Seurat 
## 16864 features across 12313 samples within 1 assay 
## Active assay: RNA (16864 features, 3000 variable features)
##  3 dimensional reductions calculated: pca, umap, tsne
table(srt$cell_type,srt$sample)
##                           
##                             yg0  yg1  yg3  yg5
##   CD11a Myeloid              89    7    9    6
##   Infiltrating Type 1       293 1371  314   46
##   Infiltrating Type 2        26 1865   46   12
##   Macrophages intermediate   15   25 2784 2131
##   Macrophages Repair         15   12 1254 1922
table(srt$sorting_day,srt$sample)
##       
##         yg0  yg1  yg3  yg5
##   day1    0 3280 4407    0
##   day2  438    0    0 4117
table(srt$lane,srt$sample)
##        
##          yg0  yg1  yg3  yg5
##   lane1    0 3280    0 4117
##   lane2  438    0 4407    0
table(srt$cell_type,srt$sorting_day)
##                           
##                            day1 day2
##   CD11a Myeloid              16   95
##   Infiltrating Type 1      1685  339
##   Infiltrating Type 2      1911   38
##   Macrophages intermediate 2809 2146
##   Macrophages Repair       1266 1937
table(srt$cell_type,srt$lane)
##                           
##                            lane1 lane2
##   CD11a Myeloid               13    98
##   Infiltrating Type 1       1417   607
##   Infiltrating Type 2       1877    72
##   Macrophages intermediate  2156  2799
##   Macrophages Repair        1934  1269
colors <- c("darkorange4","red", "yellow", "green4", "blue" )
DimPlot(srt, reduction = "tsne", group.by = "cell_type", cols = colors, pt.size = 0.5)

DimPlot(srt, reduction = "tsne", group.by = "sample", pt.size = 0.5)

DimPlot(srt, reduction = "tsne", group.by = "lane", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt, reduction = "tsne", group.by = "sorting_day", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$lane == "lane1"], reduction = "tsne", group.by = "lane", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$lane == "lane2"], reduction = "tsne", group.by = "lane", cols = c("blue"), pt.size = 0.5)

DimPlot(srt[,srt$sorting_day == "day1"], reduction = "tsne", group.by = "sorting_day", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$sorting_day == "day2"], reduction = "tsne", group.by = "sorting_day", cols = c( "blue"), pt.size = 0.5)

DimPlot(srt, reduction = "umap", group.by = "cell_type", cols = colors, pt.size = 0.5)

DimPlot(srt, reduction = "umap", group.by = "sample", pt.size = 0.5)

DimPlot(srt, reduction = "umap", group.by = "lane", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt, reduction = "umap", group.by = "sorting_day", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$lane == "lane1"], reduction = "umap", group.by = "lane", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$lane == "lane2"], reduction = "umap", group.by = "lane", cols = c("blue"), pt.size = 0.5)

DimPlot(srt[,srt$sorting_day == "day1"], reduction = "umap", group.by = "sorting_day", cols = c("orange", "blue"), pt.size = 0.5)

DimPlot(srt[,srt$sorting_day == "day2"], reduction = "umap", group.by = "sorting_day", cols = c( "blue"), pt.size = 0.5)

DimPlot(srt[,srt$sample == "yg0"], reduction = "umap", group.by = "sample", cols = c( "tomato"), pt.size = 0.5)

DimPlot(srt[,srt$sample == "yg1"], reduction = "umap", group.by = "sample", cols = c( "green3"), pt.size = 0.5)

DimPlot(srt[,srt$sample == "yg3"], reduction = "umap", group.by = "sample", cols = c( "dodgerblue"), pt.size = 0.5)

DimPlot(srt[,srt$sample == "yg5"], reduction = "umap", group.by = "sample", cols = c( "purple"), pt.size = 0.5)

mat <- GetAssayData(srt, slot = "data")
df1 <- data.frame(cell_type = srt$cell_type,
                 sample = srt$sample,
                 sorting = srt$sorting_day,
                 lane = srt$lane)

varMatrix <- getVarianceExplained(mat, variables = df1)
plotExplanatoryVariables(
            varMatrix,
            variables = variables)  + 
  ggtitle("Young - Mono/macs without resident") +
  theme(text = element_text(size=20)) +
 scale_color_manual(values=c( "dodgerblue" ,"purple" , "green3","orange", "blue", "red"))
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
## Warning in self$trans$transform(x): NaNs produced
## Warning: Transformation introduced infinite values in continuous x-axis
## Warning: Removed 197 rows containing non-finite values (`stat_density()`).

sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.6 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] scater_1.22.0               scuttle_1.4.0              
##  [3] SingleCellExperiment_1.16.0 SummarizedExperiment_1.24.0
##  [5] Biobase_2.54.0              GenomicRanges_1.46.1       
##  [7] GenomeInfoDb_1.30.1         IRanges_2.28.0             
##  [9] S4Vectors_0.32.4            BiocGenerics_0.40.0        
## [11] MatrixGenerics_1.6.0        matrixStats_0.63.0         
## [13] ggplot2_3.4.1               SeuratObject_4.1.3         
## [15] Seurat_4.1.1               
## 
## loaded via a namespace (and not attached):
##   [1] plyr_1.8.8                igraph_1.3.5             
##   [3] lazyeval_0.2.2            sp_1.6-0                 
##   [5] splines_4.1.2             BiocParallel_1.28.3      
##   [7] listenv_0.9.0             scattermore_0.8          
##   [9] digest_0.6.31             htmltools_0.5.4          
##  [11] viridis_0.6.2             fansi_1.0.4              
##  [13] magrittr_2.0.3            ScaledMatrix_1.2.0       
##  [15] tensor_1.5                cluster_2.1.4            
##  [17] ROCR_1.0-11               globals_0.16.2           
##  [19] spatstat.sparse_3.0-0     colorspace_2.1-0         
##  [21] ggrepel_0.9.3             xfun_0.37                
##  [23] dplyr_1.1.1               RCurl_1.98-1.10          
##  [25] jsonlite_1.8.4            progressr_0.13.0         
##  [27] spatstat.data_3.0-0       survival_3.5-0           
##  [29] zoo_1.8-11                glue_1.6.2               
##  [31] polyclip_1.10-4           gtable_0.3.3             
##  [33] zlibbioc_1.40.0           XVector_0.34.0           
##  [35] leiden_0.4.3              DelayedArray_0.20.0      
##  [37] BiocSingular_1.10.0       future.apply_1.10.0      
##  [39] abind_1.4-5               scales_1.2.1             
##  [41] spatstat.random_3.1-3     miniUI_0.1.1.1           
##  [43] Rcpp_1.0.10               viridisLite_0.4.1        
##  [45] xtable_1.8-4              reticulate_1.26          
##  [47] spatstat.core_2.4-4       rsvd_1.0.5               
##  [49] htmlwidgets_1.6.1         httr_1.4.4               
##  [51] RColorBrewer_1.1-3        ellipsis_0.3.2           
##  [53] ica_1.0-3                 farver_2.1.1             
##  [55] pkgconfig_2.0.3           sass_0.4.5               
##  [57] uwot_0.1.14               deldir_1.0-6             
##  [59] utf8_1.2.3                labeling_0.4.2           
##  [61] tidyselect_1.2.0          rlang_1.1.0              
##  [63] reshape2_1.4.4            later_1.3.0              
##  [65] munsell_0.5.0             tools_4.1.2              
##  [67] cachem_1.0.6              cli_3.6.1                
##  [69] generics_0.1.3            ggridges_0.5.4           
##  [71] evaluate_0.20             stringr_1.5.0            
##  [73] fastmap_1.1.0             yaml_2.3.7               
##  [75] goftest_1.2-3             knitr_1.42               
##  [77] fitdistrplus_1.1-8        purrr_1.0.1              
##  [79] RANN_2.6.1                pbapply_1.7-0            
##  [81] future_1.31.0             nlme_3.1-162             
##  [83] sparseMatrixStats_1.6.0   mime_0.12                
##  [85] compiler_4.1.2            rstudioapi_0.14          
##  [87] beeswarm_0.4.0            plotly_4.10.1            
##  [89] png_0.1-8                 spatstat.utils_3.0-1     
##  [91] tibble_3.2.1              bslib_0.4.2              
##  [93] stringi_1.7.12            highr_0.10               
##  [95] lattice_0.20-45           Matrix_1.5-3             
##  [97] vctrs_0.6.1               pillar_1.9.0             
##  [99] lifecycle_1.0.3           spatstat.geom_3.0-6      
## [101] lmtest_0.9-40             jquerylib_0.1.4          
## [103] BiocNeighbors_1.12.0      RcppAnnoy_0.0.20         
## [105] data.table_1.14.8         cowplot_1.1.1            
## [107] bitops_1.0-7              irlba_2.3.5.1            
## [109] httpuv_1.6.8              patchwork_1.1.2          
## [111] R6_2.5.1                  promises_1.2.0.1         
## [113] KernSmooth_2.23-20        gridExtra_2.3            
## [115] vipor_0.4.5               parallelly_1.34.0        
## [117] codetools_0.2-18          MASS_7.3-58.2            
## [119] withr_2.5.0               sctransform_0.3.5        
## [121] GenomeInfoDbData_1.2.7    mgcv_1.8-41              
## [123] parallel_4.1.2            beachmat_2.10.0          
## [125] grid_4.1.2                rpart_4.1.19             
## [127] tidyr_1.3.0               rmarkdown_2.20           
## [129] DelayedMatrixStats_1.16.0 Rtsne_0.16               
## [131] shiny_1.7.4               ggbeeswarm_0.7.1